#include "clib_json.h"
#include <stdio.h>
#include <assert.h>
#include <string>
#include <fstream>

using namespace std;

struct  PictureInfo {
    string name;
    float rotation;
}gPictureInfo;

void  PictureInit()
{
    clib_json::Reader reader;
    ifstream file("./test.json");
    assert(file.is_open());
    clib_json::Value root;
    if  (!reader.parse(file, root, false )) {
        printf("Parse error" );
        exit(1 );
    }

    gPictureInfo.name = root["name" ].asString();
    gPictureInfo.rotation = root["rotation" ].asDouble();
}


int main()
{
    //encode example.
    clib_json::Value  root;
    clib_json::Value  var;
    
    var["token"] = "haha";
    var["game_id"] = 1;
    
    clib_json::Value  pieces;//store all pieces
    for (int i = 0; i < 3; i++)
    {
        clib_json::Value  piece_ex;//here it store just one piece
        piece_ex["x"] = 100+i;
        piece_ex["y"] = 200+i;
        pieces.append(piece_ex);//ok,yes we just have apply One piece ,then push back to the array
    }
    
    var["piece_array"] = pieces;//yes,store pieces in var [Value]
    root.append(var);

    clib_json::FastWriter  writer;
    string strResult = writer.write(root);//generate json string:),here all is done
    
    printf("%s\n", strResult.c_str());

    
    //decode example.
    PictureInit();
    printf("%s %f\n", gPictureInfo.name.c_str(), gPictureInfo.rotation);
    
    return 0;
}

